package com.xiaoqiang.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.xiaoqiang.crm.domain.bo.CompanyBo;
import com.xiaoqiang.crm.domain.vo.CompanyVo;
import com.xiaoqiang.crm.domain.Company;
import com.xiaoqiang.crm.mapper.CompanyMapper;
import com.xiaoqiang.crm.service.ICompanyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 客户关系-公司管理Service业务层处理
 *
 * @author xiaoqiang
 * @date 2022-09-19
 */
@RequiredArgsConstructor
@Service
public class CompanyServiceImpl implements ICompanyService {

    private final CompanyMapper baseMapper;

    /**
     * 查询客户关系-公司管理
     */
    @Override
    public CompanyVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询客户关系-公司管理列表
     */
    @Override
    public TableDataInfo<CompanyVo> queryPageList(CompanyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Company> lqw = buildQueryWrapper(bo);
        Page<CompanyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询客户关系-公司管理列表
     */
    @Override
    public List<CompanyVo> queryList(CompanyBo bo) {
        LambdaQueryWrapper<Company> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Company> buildQueryWrapper(CompanyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Company> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Company::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), Company::getTel, bo.getTel());
        return lqw;
    }

    /**
     * 新增客户关系-公司管理
     */
    @Override
    public Boolean insertByBo(CompanyBo bo) {
        Company add = BeanUtil.toBean(bo, Company.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改客户关系-公司管理
     */
    @Override
    public Boolean updateByBo(CompanyBo bo) {
        Company update = BeanUtil.toBean(bo, Company.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Company entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除客户关系-公司管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
